<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>函数递归</title>
</head>
<body>
    <h1>递归</h1>
</body>
<script>
    //1+2+3...+n
    //a(5)=a(4)+5=a(3)+4=a(2)+3=a(1)
    function add(n){
        if(n==1){
            return 1;
        }else{
            return add(n-1)+n;
        }
    }
    console.log(add(5));//求1~5的累加   15

    //阶乘  5!=5*4*3*2*1
    function cheng(n){
        if(n==1){
            return 1;
        }else{
            return cheng(n-1)*n;
        }
    }
    console.log(cheng(5))//1~5的阶乘    120

    //1,1,2,3,5,8,13,.....前两个数之和得出后面这位数(斐波拉契数列)
    //f(5)=f(4)+f(3)
    //根据n求出n位数为几
    function shu(n){
        if(n==0){
            return 0;
        }else if(n==1){
            return 1;
        }else{
            return shu(n-1)+shu(n-2);
        }
    }
    console.log(shu(10))

    //模拟递归C盘所有文件
    var root={
        name:'C盘',
        children:[{
            name:'学习',
            children:[{
                name:'电子书',
                children:[
                    {
                        name:'文学',
                        children:[
                            {name:'三体'},{name:'红与黑'}
                        ]
                    }
                ]
            }
            ]
        },
        {
            name:'电影',
            children:[
                {name:'美国电影',children:[{name:'变形金刚'}]},
                {name:'中国电影',children:[{name:'长津湖'},{name:'斗罗大陆'}]}
            ]
        }]
    }
</script>
</html>